package com.lost.octopus.es.processor.entity;

import lombok.Data;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.logging.log4j.util.Strings.isBlank;

@Data
@ToString
public class OrderBy {
    private String sortField;
    private String sortType = "asc";

    public static final String SORT_FIELD = "sortField";
    /**
     * 列表排序类型倒序或者升序的key，值为desc或者asc
     */
    public static final String SORT_TYPE = "sortType";

    public OrderBy() {
    }

    public OrderBy(String sortField) {
        this.sortField = sortField;
    }

    public OrderBy(String sortField, String sortType) {
        this.sortField = sortField;
        this.sortType = sortType;
    }

    /**
     * 将orderBys转为condition
     * @return
     */
    public static Map toSqlParseCondition(List<OrderBy> orderBys) {
        if (orderBys != null && orderBys.size() > 0) {
            String sortField = "";
            String sortType = "";
            if (orderBys.size() == 1) {
                sortField = orderBys.get(0).getSortField();
                sortType = orderBys.get(0).getSortType();
            } else {
                for (int i = 0; i < orderBys.size(); i++) {
                    sortField = sortField + "," + orderBys.get(i).getSortField() + " " + orderBys.get(i).getSortType();
                    sortType = orderBys.get(i).getSortType();
                }
                sortField = sortField.substring(1);
                sortField = sortField.substring(0, sortField.lastIndexOf(" "));
            }
            String finalSortField = sortField;
            String finalSortType = sortType;
            return new HashMap<String, String>() {
                {
                    put(SORT_FIELD, finalSortField);
                    put(SORT_TYPE, finalSortType);
                }
            };
        }
        return Collections.emptyMap();
    }

    /**
     * 将condition转为orderBys
     * @return
     */
    public static List<OrderBy> of(String sortField, String sortType) {
        // sortField,setSortType
        //name desc,age,xb desc   asc
        if (isBlank(sortField)) {
            //传空就不需要排序了
            return new ArrayList<>();
        }
        if (isBlank(sortType)) {
            sortType = "asc";
        }
        String[] arr = sortField.split(",");
        List<OrderBy> orderBys = new ArrayList<OrderBy>();
        if (arr.length == 1) {
            orderBys.add(new OrderBy(arr[0], sortType));
        } else {
            //取前n-1项构造orderby
            for (int i = 0; i < arr.length - 1; i++) {
                String[] orderBy = arr[i].split(" ");
                if (orderBy.length == 1) {
                    orderBys.add(new OrderBy(orderBy[0], "asc"));
                } else {
                    orderBys.add(new OrderBy(orderBy[0], orderBy[1]));
                }
            }
            //最后一项
            orderBys.add(new OrderBy(arr[arr.length - 1], sortType));
        }
        return orderBys;
    }
}
